/**
 * Created by Xyzhao on 2017/7/13.
 */
toastr.options = {
	closeButton: true,
	debug: false,
	progressBar: true,
	positionClass: "toast-top-center",
	onclick: null,
	showDuration: "300",
	hideDuration: "1000",
	timeOut: "2000",
	extendedTimeOut: "1000",
	showEasing: "swing",
	hideEasing: "linear",
	showMethod: "slideDown",
	hideMethod: "slideUp"
};
var WEB_ROOT = $("#my-js").attr('data-root');
window.onload = function () {
	var isCap = false;
	new Vue({
		el: "form",
		data: {
			// js:false,
			Sno: '',
			Pwd: '',
			Cap: '',
			capSrc: WEB_ROOT + 'captcha.html'
		},
		methods: {
			refCap: function () {
				var capSrc = WEB_ROOT + 'captcha.html';
				this.capSrc = capSrc + '?' + 'id=' + Math.random();
			},
			chkCap: function () {
				if (this.Cap) {
					if (this.Cap.length === 4) {
						this.$http.post(WEB_ROOT + 'stuff/index/captcha.html', {
							'captcha': this.Cap
						}).then(function (res) {
							if (res.data !== true) {
								isCap = false;
								toastr.error(res.data);
							} else {
								isCap = true;
							}
						}, function () {
							isCap = false;
							toastr.warning("网络异常,请稍后再试!");
						})
					} else {
						isCap = false;
						toastr.error("验证码错误!");
					}
				}
			},
			login: function () {
				if (!this.Sno) {
					toastr.error("学号不能为空!");
				} else if (!this.Pwd) {
					toastr.error("密码不能为空!");
				} else if (!this.Cap) {
					toastr.error("验证码不能为空!");
				} else if (isCap !== true) {
					toastr.error("验证码错误!");
				} else {
					this.$http.post(WEB_ROOT + 'stuff/index/login.html', {
						'CSUid': this.Sno,
						'password': CryptoJS.SHA224(this.Pwd).toString(),
						'captcha': this.Cap
					}).then(function (res) {
							if (res.data === true) {
								window.location.href = WEB_ROOT + "stuff/index/main.html";
							} else {
								toastr.error(res.data);
							}
						}, function () {
							toastr.warning("网络异常,请稍后再试!");
						}
					)
				}
			}
		}
	})
};